home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7439 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: news.rain.org!usenet
  2. From: "Guus Leeuw jr." <guusl@eiffel.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: More Modulus questions
  5. Date: Mon, 26 Feb 1996 08:53:52 -0800
  6. Organization: Interactive Software Engineering Inc. http://www.eiffel.com/
  7. Message-ID: <3131E5A0.6134B976@eiffel.com>
  8. References: <Pine.SOL.3.90.960219171637.21117B-100000@eddie> <4gfnka$ni7@spanky.pls.ov.com> <4gfp5a$r8e@cloner2.ix.netcom.com>
  9. NNTP-Posting-Host: @outback.eiffel.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; Linux 1.2.8 i586)
  14.  
  15. KPN wrote:
  16. [snip snip]
  17. > #include <stdio.h>
  18. > main()
  19. > {
  20. >   int number;
  21. >   int first, second, third, fourth;
  22. >   int integer1, integer2, integer3, integer4;
  23. >   printf("Enter a four digit number: ");
  24. >   scanf("%d", &number);
  25. > /*
  26. >    Split the four digit number into four seperate integers
  27. > */
  28. >   first = number / 1000;
  29. >   integer1 = number % 1000;
  30. >   second = integer1 / 100;
  31. >   integer2 = integer1 % 100;
  32. >   third = integer2 / 10;
  33. >   integer3 = integer2 % 10;
  34. >   fourth = integer3 / 1;
  35. >   integer4 = integer3 % 1;
  36.  
  37. Add these two lines:
  38.     printf("%d %d %d %d\n", first, second, third, fourth);
  39.     printf("%d %d %d %d\n", first%7, second%7, third%7, fourth%7);
  40.  
  41. > /*
  42. >    Use modulas to determine if any integers are a 7
  43. > */
  44. >    if (first % 7)
  45. >       printf("First integer was a 7\n");
  46. >    else
  47. >       printf("Not a 7\n");
  48. > if (second % 7)
  49. >       printf("second integer was a 7\n");
  50. >    else
  51. >       printf("Not a 7\n");
  52. > if (third % 7)
  53. >       printf("third integer was a 7\n");
  54. >    else
  55. >       printf("Not a 7\n");
  56. > if (fourth % 7)
  57. >       printf("fourth integer was a 7\n");
  58. >    else
  59. >       printf("Not a 7\n");
  60. [snip snip]
  61.  
  62. From the added printf statements, you can see that something is wrotten.
  63. 7%7 gives 0. Modulo is the remainder after division.
  64. You're test whether 7%7 is not 0, which is wrong.
  65.  
  66. 0%77 will also give 0, and that's something you really don't want.
  67.  
  68. I think, there's just one way to determine whether 7 is 7 and that is either `7 == 7'
  69. or '7 % 10 == 7'.
  70.  
  71. In the above text each first 7 in each expression is to be replaced with `first' ..
  72. `fourth'.
  73.  
  74. Hope this helps,
  75.     Guus
  76.